﻿using furion.blog.Domain.IRepositories;
using furion.blog.Domain.MySQL;
using furion.blog.Domain.Query.Feedback;
using furion.blog.Domain.Shared;
using Furion.DependencyInjection;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace furion.blog.EntityFramework.Core.Repositories
{
    public class FeedbackRepository : BaseSqlSugarRepository<Feedback>, IFeedbackRepository, ISingleton
    {
        public readonly ISqlSugarClient _db;
        public FeedbackRepository(ISqlSugarClient db) {
            this._db = db;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> Save(Feedback model) {
            return await base.InsertAsync(model);
        }


        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<PageVO> List(FeedbackListQuery query) {
            try
            {
                var (strWhere, sqlParams) = query.QueryParams();
                long startIndex = query.GetStartIndex();

                string countSql = query.GetCountSQL();
                string dataSql = query.GetDataSQL();

                long total = await this._db.Ado.GetLongAsync(countSql, sqlParams);
                var data = await this._db.Ado.GetDataTableAsync(dataSql, sqlParams);
                return new PageVO { Total = total, Records = data };
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return new PageVO { Total = 0L, Records = null };
        }


        /// <summary>
        /// 通过id查找
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Feedback> FindById(long id) {
            return await base.GetByIdAsync(id);
        }


        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> Update(Feedback model) {
            return await base.UpdateAsync(model);
        }

    }
}
